package com.weasing.res.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.weasing.res.domain.WxUserdetail;

/**
 * 文件生成的工具类
 * 
 * @author 黄龙
 *
 */
@SuppressWarnings("deprecation")
public class FileUtils {

	private static String shenpibiaoPath = RESUtils.getRequest()
			.getRealPath("//WEB-INF//docModel//shenpibiao.docx");
	private static String juedingshuPath = RESUtils.getRequest()
			.getRealPath("//WEB-INF//docModel//daibujuedingshu.docx");

	private static String destfilePath = RESUtils.getRequest()
			.getRealPath("//WEB-INF//evalRecord");
	
	/**
	 * 生成审批表的副本
	 * 
	 * @author 黄龙
	 * @param map是模板参数
	 * @return 返回的是文件副本绝对地址
	 */
	public static String creatEvalresult(Map<String,String> map,
			WxUserdetail userdetail) {
		String destPath = destfilePath + File.separator + userdetail.getId()
				+ "-" + "shenpibiao.doc";
		// 替换数据,并且生成临时文件
		WordTemplate.replaceDocument(shenpibiaoPath, destPath, map);
		return destPath;
	}

	/**
	 * 生成逮捕表的副本
	 * 
	 * @author 黄龙
	 * @param map是模板参数
	 * @return 返回的是文件副本绝对地址
	 */
	public static String creatDaibubiao(Map<String, String> map,
			WxUserdetail userdetail) {
		String destPath = destfilePath + File.separator + userdetail.getId()
				+ "-" + "daibujuedingshu.doc";
		System.out.println(destPath);
		// 替换数据,并且生成临时文件
		WordTemplate.replaceDocument(juedingshuPath, destPath, map);
		return destPath;
	}

	/**
	 * 生成下载的链接
	 * 
	 * @Parameters
	 * 
	 * 			参数是文件名xxxx.doc
	 * 
	 * 
	 * 
	 */
	public static void downLoad(String name, String chargename) {
		String filePath = destfilePath + File.separator + name;
		// 就用这个
		// String filePath = destfilePath + "//羁押必要性审查案件审批表.docx";
		HttpServletResponse response = RESUtils.getResponse();
		ServletOutputStream out = null;
		InputStream fin = null;
		try {
			String filename = filePath.substring(filePath.lastIndexOf("/") + 1);
			if (filename.contains("shenpibiao")) {
				filename = filename.replace("shenpibiao",
						chargename + "-羁押必要性审查案件审批表");
			}
			if(filename.contains("jiyabiao")){
				filename = filename.replace("jiyabiao", chargename + "-逮捕决定书");
			}
			
			
			filename = new String(filename.getBytes("UTF-8"));

			String path = filePath;
			File file = new File(path);
			// 如果文件不存在
			if (!file.exists()) {
				return;
			}
			// 设置响应头，控制浏览器下载该文件
			response.setHeader("content-disposition", "attachment;filename="
					+ URLEncoder.encode(filename, "UTF-8"));
			// 读取要下载的文件，保存到文件输入流
			FileInputStream in = new FileInputStream(path);
			// 创建输出流
			out = response.getOutputStream();
			// 缓存区
			byte buffer[] = new byte[1024];
			int len = 0;
			// 循环将输入流中的内容读取到缓冲区中
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
			// 关闭
			in.close();
			out.close();

		} catch (Exception e) {
			System.out.println("error");
			e.printStackTrace();
		}

		finally {
			if (fin != null)
				try {
					fin.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}


}
